#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "ini.h"
#include "register.h"

void fillSection1Info(const char* name, const char* value, void* data)
{
    Section1Data* section1_data = (Section1Data*)data;
    if (strcmp(name, "key1") == 0) {
        strncpy(section1_data->key1, value, sizeof(section1_data->key1));
    } else if (strcmp(name, "key2") == 0) {
        section1_data->key2 = atoi(value);
    }
}

// ini文件的回调函数
int handler(void* user, const char* section, const char* name, const char* value) 
{
    for (int i = 0; i < section_count; i++) {
        if (strcmp(section, sections[i].name) == 0) {
            if (strcmp(section, "baseInfo") == 0) {
                fillBaseInfo(name, value);
            } else if (strcmp(section, "cmd0") == 0) {
                // do nothing
            } else if (strcmp(section, "cmd1") == 0) {
                fillSection1Info(name, value, sections[i].data);
            } else if (strcmp(section, "cmd2") == 0) {
                parse_hex_string(value, sections[i].data, sizeof(Section2Data));
            }
            break;
        }
    }
    return 1; // 返回1表示成功处理
}

int parseIni()
{
    // 读取ini文件
    if (ini_parse("config.ini", handler, NULL) < 0) {
        printf("Error: Cannot load 'config.ini'\n");
        return 1;
    }
}
